<?php
/**
 * lsys orm
 * @author     Lonely <shan.liu@msn.com>
 * @copyright  (c) 2017 Lonely <shan.liu@msn.com>
 * @license    http://www.apache.org/licenses/LICENSE-2.0
 */
namespace LSYS\ORM;
use LSYS\ORM;
class Result extends \LSYS\Database\Result{
	/**
	 * @var Result
	 */
	protected $_result;
	public function __construct(\LSYS\Database\Result $result){
		$this->_result=$result;
	}
	/**
	 * set fetch data mode
	 * @param int $mode
	 * @param string $classname
	 * @param array $ctorargs
	 */
	public function set_fetch_mode ( $mode,$classname=NULL, array $ctorargs=NULL){
		throw new Exception("not support set_fetch_mode mothod");
	}
	/**
	 * Result destruction cleans up all open result sets.
	 *
	 * @return  void
	 */
	public function __destruct(){
		return call_user_func_array(array($this->_result,'__destruct'), func_get_args());
	}
	/**
	 * Return all of the rows in the result as an array.
	 *
	 *     // Indexed array of all rows
	 *     $rows = $result->as_array();
	 *
	 *     // Associative array of rows by "id"
	 *     $rows = $result->as_array('id');
	 *
	 *     // Associative array of rows, "id" => "name"
	 *     $rows = $result->as_array('id', 'name');
	 *
	 * @param   string  $key    column for associative keys
	 * @param   string  $value  column for values
	 * @return  array
	 */
	public function as_array($key = NULL, $value = NULL)
	{
		$result=call_user_func_array(array($this->_result,'as_array'), func_get_args());
		foreach ($result as $k=>$v){
			if ($v instanceof Entity) $result[$k]=$v->as_array();
		}
		return $result;
	}
	/**
	 * Return the named column from the current row.
	 *
	 *     // Get the "id" value
	 *     $id = $result->get('id');
	 *
	 * @param   string  $name     column to get
	 * @param   mixed   $default  default value if the column does not exist
	 * @return  mixed
	 */
	public function get($name, $default = NULL)
	{
		return call_user_func_array(array($this->_result,'get'), func_get_args());
	}
	
	/**
	 * Implements [Countable::count], returns the total number of rows.
	 *
	 *     echo count($result);
	 *
	 * @return  integer
	 */
	public function count()
	{
		return call_user_func_array(array($this->_result,'count'), func_get_args());
	}
	
	/**
	 * Implements [ArrayAccess::offsetExists], determines if row exists.
	 *
	 *     if (isset($result[10]))
	 	*     {
	 	*         // Row 10 exists
	 	*     }
	 *
	 * @param   int     $offset
	 * @return  boolean
	 */
	 public function offsetExists($offset)
	 {
	 	return call_user_func_array(array($this->_result,'offsetExists'), func_get_args());
	 }
	
	 /**
	  * Implements [ArrayAccess::offsetGet], gets a given row.
	  *
	  *     $row = $result[10];
	  *
	  * @param   int     $offset
	  * @return  mixed
	  */
	 public function offsetGet($offset)
	 {
	 	return call_user_func_array(array($this->_result,'offsetGet'), func_get_args());
	 }
	 /**
	  * Implements [Iterator::key], returns the current row number.
	  *
	  *     echo key($result);
	  *
	  * @return  integer
	  */
	 public function key()
	 {
	 	return call_user_func_array(array($this->_result,'key'), func_get_args());
	 }
	
	 /**
	  * Implements [Iterator::next], moves to the next row.
	  *
	  *     next($result);
	  *
	  * @return  $this
	  */
	 public function next()
	 {
	 	return call_user_func_array(array($this->_result,'next'), func_get_args());
	 }
	
	 /**
	  * Implements [Iterator::prev], moves to the previous row.
	  *
	  *     prev($result);
	  *
	  * @return  $this
	  */
	 public function prev()
	 {
	 	return call_user_func_array(array($this->_result,'prev'), func_get_args());
	 }
	
	 /**
	  * Implements [Iterator::rewind], sets the current row to zero.
	  *
	  *     rewind($result);
	  *
	  * @return  $this
	  */
	 public function rewind()
	 {
	 	return call_user_func_array(array($this->_result,'rewind'), func_get_args());
	 }
	
	 /**
	  * Implements [Iterator::valid], checks if the current row exists.
	  *
	  * [!!] This method is only used internally.
	  *
	  * @return  boolean
	  */
	 public function valid()
	 {
	 	return call_user_func_array(array($this->_result,'valid'), func_get_args());
	 }
	 /**
	  * Implements [Iterator::valid], checks if the current row exists.
	  *
	  * [!!] This method is only used internally.
	  *
	  * @return  boolean
	  */
	 public function seek($position)
	 {
	 	return call_user_func_array(array($this->_result,'seek'), func_get_args());
	 }
	 /**
	  * {@inheritDoc}
	  * @return ORM
	  */
	 public function current()
	 {
	 	return call_user_func_array(array($this->_result,'current'), func_get_args());
	 }
}